function ajax(url, para, success_cb, error){
	if(para === null) {
	  	para = {}
	}
	
	url = 'http://118.190.64.115:9090/fishing' + url
	
	/*para.token = getQueryString("token")
	if(getQueryString("u_id") != null){
		para.u_id = parseInt(getQueryString("u_id"))
	}
	  
	if(typeof(para.m_id)!="undefined"){
	    para.m_id = parseInt(getQueryString("u_id"))
	}
	var timestamp = Date.parse(new Date()) / 1000;
	para.timestamp = timestamp.toString()
	var str = acciiSort(para)
	console.log(str.split('&').join(''))
	para.sign = hex_md5((str.split('&').join('')))
	console.log(para)*/
	
	$.ajax({
	    url: url,
	    type : "post",
	    data : para,
	    async: false,
    	dataType: 'json',
    	crossDomain: true,
    	beforeSend: function(){
	     // Handle the beforeSend event
	    },
	    complete: function(){
	     	// Handle the complete event
	    },
	    success : function(res){
	    	
	    	if(res != null && res.rs != null && res.code == 0 && res.msg == "SUCCESS"){
	    		if(success_cb != null) success_cb(res.rs);
	    	}else{
	    		/*if(res == null || res.msg == null){
	    			if(error != null) error(res)
	    		}else{
	    			if(error != null) error(res)
	    		}*/
				toast(res.msg, 3)
				if(error != null) error(res)
	    	}
	    },
	    error : function(res){
	    	if(error != null) error(res)
	    	toast("系统错误，请联系管理员", 2)
			
			if(error != null) error(res)
	    }
	})
}

function acciiSort (oldObj) {
    var result = ''
    var sdic = Object.keys(oldObj).sort()
    for (var i = 0; i < sdic.length; i++) {
      result += sdic[i] + '=' + oldObj[sdic[i]] + '&'
    }
    return result.substring(0, result.length - 1)
}

function getQueryString(name) { 
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r != null) return unescape(r[2]); return null; 
} 

function login(url){
	//$.cookie("qudiaoyu_u","1111",{domain: 'www.acb.com'})
	ajax(api.login, null, function(res){
		console.log("登陆成功")
	}, function(res){
		if(res.code != 0){
			console.warn(res)
			window.location.href="http://localhost/fishing/site/user/login?url="+url
		}
	})
}

function expOrder(p_id, sku_id, success_cb){
	//$.cookie("qudiaoyu_u","1111",{domain: 'www.acb.com'})
	ajax(api.checkProductState, {product_id: p_id, sku_id: sku_id}, function(res){
		if(success_cb != null) success_cb()
	}, function(res){
		return true
	})
}

function toast(msg, type){
	var m_type = null;
	
	if(type != null){
		if(type == 2){
			m_type = "cancel"
		}else{
			m_type = "forbidden"
		}
	}
	
	if(m_type == null){
		$.toast(msg)
		
	}else{
		$.toast(msg, m_type)
	}
	
	
	setTimeout(function(){
		$(".weui-toast").remove()
	},2500)
}

function confirm(msg,title,success,cancel){
	$.confirm(msg, title, function() {
		$(".weui-dialog").remove()
		$(".weui-mask").remove()
		if(success != null) success()
	},function(){
		$(".weui-dialog").remove()
		$(".weui-mask").remove()
		if(cancel != null) cancel()
	})
}
